package com.lightsaber.trade.cloth.web.rollover;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springside.modules.orm.Page;

import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.web.BaseAction;
import com.lightsaber.trade.cloth.entity.archive.ArchivedImage;
import com.lightsaber.trade.cloth.entity.category.Category;
import com.lightsaber.trade.cloth.entity.planning.Planning;
import com.lightsaber.trade.cloth.entity.planning.PlanningLevel;
import com.lightsaber.trade.cloth.service.category.CategoryService;
import com.lightsaber.trade.cloth.service.planning.PlanningService;
import com.lightsaber.trade.cloth.service.rollover.RolloverImageService;

@Namespace("/rollover")
@Results({ @Result(name = "rollover-level2", location = "rollover-level2.jsp") })
public class RolloverAction extends BaseAction {
    private static final long serialVersionUID = -7076736990225879052L;

    @Autowired
    private RolloverImageService rolloverImageService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private PlanningService planningService;

    private ArchivedImage archivedImage;
    private Integer planningExercise;
    private String planningFirstStatus;
    private String planningIdSources;
    private String planningId;
    private String remarks;

    // ****************** Page parameters *****************START
    private boolean search;
    @Value(value = "#{appProperties['rollover.page.size']}")
    private String pageSize;
    private Page<ArchivedImage> page;

    // ****************** Page parameters *****************END

    @Override
    public String execute() {
        return list();
    }

    public String list() {
        if (search) {
            if (page == null) {
                page = new Page<ArchivedImage>(Integer.valueOf(pageSize));
            } else {
                page.setPageSize(Integer.valueOf(pageSize));
            }

            if (!page.isOrderBySetted()) {
                page.setOrderBy(BaseEntity.ID);
                page.setOrder(Page.ASC);
            }
            rolloverImageService.findPage(archivedImage, page);

        } else {
            archivedImage = null;
        }
        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        archivedImage = rolloverImageService.get(archivedImage.getId());
        return INPUT;
    }

    public String inputLevel2() throws Exception {

        archivedImage = rolloverImageService.get(archivedImage.getId());
        Category category = categoryService.findMasterTreeByPlanYear(archivedImage
                .getPlanningExercise() + 1);
        if (category == null) {
            this.addFieldError(
                    "rolloverLevel1First",
                    "Please rollover planning level 1 first of Planning Exercise:"
                            + archivedImage.getPlanningExercise());
            ArchivedImage archivedImageTemp = new ArchivedImage();
            Planning planningTemp = new Planning();
            planningTemp.setPlanningLevel(PlanningLevel.FirstLevel);
            archivedImageTemp.setPlanning(planningTemp);
            archivedImageTemp.setPlanningExercise(archivedImage.getPlanningExercise());
            archivedImage = archivedImageTemp;
            search = true;
            list();
            return SUCCESS;
        } else {
            planningExercise = category.getPlanYear();
            planningIdSources = archivedImage.getPlanning().getId();
            planningFirstStatus = planningService
                    .findByCategoryIdAndPlanningLevel(category.getId(), PlanningLevel.FirstLevel)
                    .getStatus().getLabel();
            planningId = planningService.findByCategoryIdAndPlanningLevel(category.getId(),
                    PlanningLevel.SecondLevel).getId();
        }
        return "rollover-level2";
    }

    public String rollover() throws Exception {
        rolloverImageService.rolloverLevel2(planningIdSources, planningId, remarks);

        addActionMessage(getText(PROCESSED_SUCCESS));

        return "rollover-level2";
    }

    public ArchivedImage getArchivedImage() {
        return archivedImage;
    }

    public void setArchivedImage(final ArchivedImage archivedImage) {
        this.archivedImage = archivedImage;
    }

    public boolean isSearch() {
        return search;
    }

    public void setSearch(final boolean search) {
        this.search = search;
    }

    public Page<ArchivedImage> getPage() {
        return page;
    }

    public void setPage(final Page<ArchivedImage> page) {
        this.page = page;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(final CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public String getPlanningFirstStatus() {
        return planningFirstStatus;
    }

    public void setPlanningFirstStatus(final String planningFirstStatus) {
        this.planningFirstStatus = planningFirstStatus;
    }

    public Integer getPlanningExercise() {
        return planningExercise;
    }

    public void setPlanningExercise(final Integer planningExercise) {
        this.planningExercise = planningExercise;
    }

    public String getPlanningId() {
        return planningId;
    }

    public void setPlanningId(final String planningId) {
        this.planningId = planningId;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(final String remarks) {
        this.remarks = remarks;
    }

    public String getPlanningIdSources() {
        return planningIdSources;
    }

    public void setPlanningIdSources(final String planningIdSources) {
        this.planningIdSources = planningIdSources;
    }

}
